package cn.htsyb.synergy.orm.jpa.model.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.persistence.Column;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.MappedSuperclass;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.Formula;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;

/**
 *  实体(标准)抽象类
 * @author lakeside hubin_yuan.410@hotmail.com
 * @version V1.0.0
 * @since 1.0
 * @date 2022/5/11
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
@Setter
@Getter
@NoArgsConstructor
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
@Schema(description = "实体(标准)抽象类")
public abstract class AbsStandardEntity extends AbsIdStatusEntity {
    private static final long serialVersionUID = 8951782271648115819L;
    /**
     * 创建时间
     */
    @CreatedDate
    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    @Column(name = "created_date", updatable = false, columnDefinition = "datetime COMMENT '创建时间'")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Schema(name = "createdDate", description = "创建时间")
    protected Date createdDate;

    /**
     * 创建者编号(此处请使用userId)
     */
    @CreatedBy
    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    @Column(name = "created_by", columnDefinition = "varchar(36) COMMENT '创建者'")
    @Schema(name = "createdBy", description = "创建者编号")
    protected String createdBy;

    /**
     * 修改时间
     */
    @LastModifiedDate
    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    @Column(name = "modified_date", columnDefinition = "datetime COMMENT '最后修改时间'")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Schema(name = "modifiedDate", description = "修改时间")
    protected Date modifiedDate;

    /**
     * 修改者编号(此处请使用userId)
     */
    @LastModifiedBy
    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    @Column(name = "modified_by", columnDefinition = "varchar(36) COMMENT '最后修改者'")
    @Schema(name = "modifiedBy", description = "修改者编号")
    protected String modifiedBy;

    /**
     * 备注描述
     */
    @Column(name = "remark_note", columnDefinition = "varchar(500) COMMENT '备注描述'")
    @Schema(name = "remarkNote", description = "备注描述")
    protected String remarkNote;
    /**
     * 排序序号
     */
    @Column(name = "sort_num", length = 4, columnDefinition = " INT(4) COMMENT '排序序号' ")
    @Schema(name = "sortNum", description = "排序")
    protected Integer sortNum;
    /**
     * 创建者回显
     */
    @Formula("(SELECT ha.real_name FROM security_user ha WHERE ha.id_= created_by)")
    private String createdByName;
    /**
     * 修改者回显
     */
    @Formula("(SELECT ha.real_name FROM security_user ha WHERE ha.id_= modified_by)")
    private String modifiedByName;
}
